<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\Room;
use App\Http\Model\RoomUser;
use App\Http\Model\SystemConfig;
use App\Http\Services\GiftServices;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\RoomUserServices;
use App\Http\Services\UserServices;
use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Services\RedPacketRainServices;
use App\Http\Model\RoomMicro;
use App\Http\Services\NewRoomMicroServices;

class RoomUserController extends Controller
{
    private $services_room_user;
    private $services_user;
    private $services_room_micro;
    private $services_room_msg;
    private $services_room_waitbit;
    private $services_room_hostmicro;
    private $services_diamond_gift;
    protected $redPacketRainService;
    protected $roomMicroModel;
    protected $newRoomMicroService;
    protected $room_user_services;

    public function __construct(UserServices $userServices, RoomMsgServices $roomMsgServices, GiftServices $giftServices)
    {
        $this->services_room_user = RoomServicesInit::createRoomUser();
        $this->services_user = $userServices;
        $this->services_room_micro = RoomServicesInit::createMicro();
        $this->services_room_msg = $roomMsgServices;
        $this->services_room_waitbit = RoomServicesInit::createWaitbit();
        $this->services_room_hostmicro = RoomServicesInit::createHostMicro();
        $this->services_diamond_gift = $giftServices;
        $this->redPacketRainService = new RedPacketRainServices();
        $this->roomMicroModel = new RoomMicro();
        $this->newRoomMicroService = new NewRoomMicroServices();
        $this->room_user_services = new RoomUserServices();

    }

    /**
     * 获取房间内用户
     * @param Request $request
     */
    public function get_users(Request $request)
    {
        if (!$request->get("room_id")) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }

        $room_id = $request->get("room_id");
        $guardTypeId = $request->get("guard_type") ?? -1;
        $data = $this->services_room_user->get_users($room_id, $guardTypeId);
        if (empty($data)) {
            OutJson::outJsonResult(Hint::SUCCESS, []);
        }

        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取房间内用户, v2版本
     * @param Request $request
     */
    public function get_users_v2(Request $request)
    {
        if (!$request->get("room_id")) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $room_id = intval($request->get("room_id"));
        $guardTypeId = $request->get("guard_type") ?? -1;//电台房用到(暂时无用)
        $page = intval($request->get('page', 0));//第1页传0
        $pageSize = intval($request->get('pageSize', 20));//每页多少条
        $data = $this->services_room_user->get_users_v2($room_id, intval($guardTypeId), $page, $pageSize);
        if (empty($data)) {
            Response::returnResult(Hint::SUCCESS, []);
        }

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 进入房间
     * @param Request $request
     */
    public function join(Request $request)
    {
        if (!$request->get("room_id")) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $room_id = $request->get("room_id");
        //获取UA值
        $header = $request->userAgent();

        // 清理之前房间
        $user = $this->services_user->get_current_user();
        $user_id = null;
        if (!empty($user)) {
            $user_id = $user['user_id'];
            $old_room = $this->services_room_user->get_cur_room($user_id);
        }

        //判断用户是否锁麦
        $microLock = $this->roomMicroModel->getRoomLockMicroByUserId($user_id);
        if(!empty($microLock)) {
            if($microLock['room_id'] != $room_id) {
                Response::returnResult(Hint::ROOM_MICRO_USER_LOCK, [], '您已锁麦，请下麦后进入其他房间');
            }
            $old_room = [];
        }

        //校验
        if (!empty($old_room)) {
            $checkRes = $this->newRoomMicroService->checkRoomUserCanQuit($old_room['room_id'], $user_id);
            if($checkRes['code'] != Hint::SUCCESS) {
                $old_room = [];
            }
        }

        if (!empty($old_room)) {
            $old_room_id = $old_room['room_id'];
            $this->services_room_user->callback_del($old_room_id, $user_id);
            $micro_id = null;
            if ($this->services_room_micro->callback_out($old_room_id, $user_id, $micro_id)) {
                $this->services_room_msg->broadcast_micro_out($old_room_id, $micro_id, $user_id);
                $this->services_room_micro->unmark_gift($old_room_id, $micro_id);
                $this->services_room_msg->broadcast_micro_ungift($old_room_id, $micro_id);
            }
            $this->services_room_waitbit->callback_del($old_room_id, $user_id);
            $this->services_room_msg->broadcast_micro_waitbit_del($old_room_id, $user_id);
            $timestamp = 0;
            if ($this->services_room_hostmicro->callback_out($old_room_id, $user_id, $timestamp)) {
                $this->services_room_msg->broadcast_hostmicro_out($old_room_id, 0, $timestamp, $user_id);
                $this->services_room_hostmicro->unmark_gift($old_room_id);
                $this->services_room_msg->broadcast_hostmicro_ungift($old_room_id, 0);
                $this->services_room_msg->broadcastHostMicroPearlUngift($old_room_id, 0);
            }
            $this->services_room_msg->broadcast_user_out($old_room_id, $user_id);

        }

        $password = $request->get('password');

        if ($password !== null) {
            $password = trim($password);
        }
        if ($password === '') {
            $password = null;
        }
        $data = $this->services_room_user->add($room_id, $password, $header);
        if($data === Hint::SECRET_ROOM_CAN_NOT_PLAY){
            Response::returnResult(Hint::SECRET_ROOM_CAN_NOT_PLAY);
        }
        if ($data === null) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($data === Hint::ROOM_JOIN_ERROR){
            Response::returnResult(Hint::ROOM_JOIN_ERROR);
        }

        if($data === Hint::ROOM_JOIN_ERROR2){
            Response::returnResult(Hint::ROOM_JOIN_ERROR2);
        }

        if ($data === Hint::ROOM_NO_EXIST) {
            Response::returnResult(Hint::ROOM_NO_EXIST);
        }
        if ($data === Hint::ERR_FAIL) {
            Response::returnResult(Hint::ERR_FAIL);
        }
        if ($data === Hint::ROOM_USER_IS_EXIST) {
            Response::returnResult(Hint::ROOM_USER_IS_EXIST);
        }
        if ($data === Hint::ROOM_USER_SHOT_OFF) {
            Response::returnResult(Hint::ROOM_USER_SHOT_OFF);
        }
        if ($data === Hint::ROOM_NOT_OPEN) {
            Response::returnResult(Hint::ROOM_NOT_OPEN);
        }

        if ($data === Hint::ROOM_PASSWORD_NEED) {
            Response::returnResult(Hint::ROOM_PASSWORD_NEED);
        }

        if ($data === Hint::ROOM_PASSWORD_ERR) {
            Response::returnResult(Hint::ROOM_PASSWORD_ERR);
        }

        if ($data === Hint::USER_NOT_CERTIFICATION) {
            Response::returnResult(Hint::USER_NOT_CERTIFICATION);
        }

        if ($data === Hint::USER_NOT_ADULT) {
            Response::returnResult(Hint::USER_NOT_ADULT);
        }

        if ($data === Hint::NEW_VERSION_MSG) {
            Response::returnResult(Hint::NEW_VERSION_MSG);
        }

        if ($data === Hint::ROOM_THREE_SPACE_USER_NUM_MSG) {
            Response::returnResult(Hint::ROOM_THREE_SPACE_USER_NUM_MSG);
        }

        if ($data === Hint::WAIT_OPEN) {
            Response::returnResult(Hint::WAIT_OPEN);
        }

        if ($data === Hint::VERSION_UPDATE_NOTICE) {
            Response::returnResult(Hint::VERSION_UPDATE_NOTICE);
        }

        if ($data === Hint::ROOM_RECTIFY) {
            Response::returnResult(Hint::ROOM_RECTIFY);
        }

        //红包雨
        $redPacketRain = $this->redPacketRainService->redPacketRainGetNewly($user_id, $room_id);
        $data['redPacketRain'] = $redPacketRain['data'];
        $data['now'] = time();

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 重新进入房间
     * @param Request $request
     * @return void
     */
    public function joinAgain(Request $request) {
        if (!$request->get("room_id")) {
            Response::returnResult(Hint::PARAM_ERROR);
            return;
        }

        $room_id = $request->get("room_id");
        //获取UA值
        $header = $request->userAgent();

        // 清理之前房间
        $user = $this->services_user->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $user_id = $user['user_id'];

        //判断用户是否锁麦
        $microLock = $this->roomMicroModel->getRoomLockMicroByUserId($user_id);
        if(!empty($microLock)) {
            if($microLock['room_id'] != $room_id) {
                Response::returnResult(Hint::ROOM_MICRO_USER_LOCK, [], '您已锁麦，请下麦后进入其他房间');
            }
        }

        $old_room = $this->services_room_user->get_cur_room($user_id);

        //校验
        if (!empty($old_room)) {
            if ($old_room['room_id'] == $room_id) {
                Response::returnResult(Hint::ROOM_USER_IS_EXIST);
            } else {
                Response::returnResult(Hint::ERROR, [], 'room id not match');
            }
        }

        $data = $this->services_room_user->addNoPwd($room_id, $header);
        if($data === Hint::SECRET_ROOM_CAN_NOT_PLAY){
            Response::returnResult(Hint::SECRET_ROOM_CAN_NOT_PLAY);
        }
        if ($data === null) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($data === Hint::ROOM_JOIN_ERROR){
            Response::returnResult(Hint::ROOM_JOIN_ERROR);
        }

        if($data === Hint::ROOM_JOIN_ERROR2){
            Response::returnResult(Hint::ROOM_JOIN_ERROR2);
        }

        if ($data === Hint::ROOM_NO_EXIST) {
            Response::returnResult(Hint::ROOM_NO_EXIST);
        }
        if ($data === Hint::ERR_FAIL) {
            Response::returnResult(Hint::ERR_FAIL);
        }
        if ($data === Hint::ROOM_USER_IS_EXIST) {
            Response::returnResult(Hint::ROOM_USER_IS_EXIST);
        }
        if ($data === Hint::ROOM_USER_SHOT_OFF) {
            Response::returnResult(Hint::ROOM_USER_SHOT_OFF);
        }
        if ($data === Hint::ROOM_NOT_OPEN) {
            Response::returnResult(Hint::ROOM_NOT_OPEN);
        }

        if ($data === Hint::ROOM_PASSWORD_NEED) {
            Response::returnResult(Hint::ROOM_PASSWORD_NEED);
        }

        if ($data === Hint::ROOM_PASSWORD_ERR) {
            Response::returnResult(Hint::ROOM_PASSWORD_ERR);
        }

        if ($data === Hint::USER_NOT_CERTIFICATION) {
            Response::returnResult(Hint::USER_NOT_CERTIFICATION);
        }

        if ($data === Hint::USER_NOT_ADULT) {
            Response::returnResult(Hint::USER_NOT_ADULT);
        }

        if ($data === Hint::NEW_VERSION_MSG) {
            Response::returnResult(Hint::NEW_VERSION_MSG);
        }

        if ($data === Hint::ROOM_THREE_SPACE_USER_NUM_MSG) {
            Response::returnResult(Hint::ROOM_THREE_SPACE_USER_NUM_MSG);
        }

        if ($data === Hint::WAIT_OPEN) {
            Response::returnResult(Hint::WAIT_OPEN);
        }

        if ($data === Hint::VERSION_UPDATE_NOTICE) {
            Response::returnResult(Hint::VERSION_UPDATE_NOTICE);
        }

        if ($data === Hint::ROOM_RECTIFY) {
            Response::returnResult(Hint::ROOM_RECTIFY);
        }

        //进入房间
        $this->room_user_services->callback_user_add($room_id, $user_id);

        //红包雨
        $redPacketRain = $this->redPacketRainService->redPacketRainGetNewly($user_id, $room_id);
        $data['redPacketRain'] = $redPacketRain['data'];
        $data['now'] = time();

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 离开房间
     * @param Request $request
     */
    public function quit(Request $request)
    {
        OutJson::outJsonNoData(Hint::SUCCESS);
        return;
        if (!$request->get("room_id")) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $room_id = $request->get("room_id");
        $data = $this->services_room_user->del($room_id);
        if ($data === null) {
            OutJson::outJsonNoData(Hint::LOGIN_OUT);
            return;
        }

        if ($data === Hint::ROOM_USER_NOT_EXIST) {
            OutJson::outJsonNoData(Hint::ROOM_USER_NOT_EXIST);
            return;
        }
        OutJson::outJsonNoData(Hint::SUCCESS);
    }


    /**
     * 获用户当前所在房间
     * @param Request $request
     */
    public function get_cur_room(Request $request)
    {
        $user_id = $request->get("user_id");
        if ($user_id == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_user->get_cur_room($user_id);

        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 用户是否在房间内
     * @param Request $request
     */
    public function user_exist(Request $request)
    {
        if (!$request->get("room_id")) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $room_id = $request->get("room_id");
        $exist = $this->services_room_user->user_exist($room_id);
        if (!$exist) {
            Response::returnResult(Hint::ROOM_USER_NOT_EXIST);

        }
        Response::returnResult(Hint::SUCCESS);
    }


    /*
   * 获取可访问房间类型接口
   * */
    public function getRoomAllowType(Request $request)
    {
        $typeArr = [0, 1];
        $data = $this->services_room_user->show_room_allow_type($typeArr);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }


    /*
       * 设置18岁禁 及 未认证
       * */
    public function setRoomAllowType(Request $request)
    {
        $type = $request->post('type');
        if (!isset($type)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
        }
        $typeStr = $request->post('typeJson');
        if (!isset($typeStr)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
        }
        $this->services_room_user->set_room_allow_type($type, $typeStr);
        OutJson::outJsonNoData(Hint::SUCCESS);
    }

}